package com.lottery.game.runner;

import com.alibaba.fastjson.JSONObject;
import com.lottery.game.common.constants.GameConstants;
import com.lottery.game.core.modules.entity.game.GameRoom;
import com.lottery.game.core.modules.entity.user.ChatRecord;
import com.lottery.game.websocket.MyHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 用GamblingClosuresExecutor替换PushMsgThread
 * @author Administrator
 *
 */
@Deprecated
public class PushMsgThread extends Thread{

	private static final Logger logger = LoggerFactory.getLogger(PushMsgThread.class);

	private List<GameRoom> roomList;
	private String msg;

	public PushMsgThread( List<GameRoom> roomList,String msg) {
        this.roomList = roomList;
        this.msg = msg;
    }

	/**
     * 推送封盘消息
     * @authoer zhuiyv
     * @date 2018-06-20
     */
	@Override
	public void run() {
	    //
	    logger.info("推送消息线程启动,msg:"+msg);
	    //对每个房间推送mgs消息
	    for(int i=0;i<roomList.size();i++) {
            String roomNum = roomList.get(i).getRoomnumber();
            Map<String, WebSocketSession> map = MyHandler.webSocketMap.get(roomNum);
            if(map!=null&&map.entrySet()!=null) {
                Iterator<Entry<String, WebSocketSession>> it = map.entrySet().iterator();
                while(it.hasNext()) {
                    WebSocketSession session = it.next().getValue();
                    ChatRecord chatRecord = new ChatRecord();
                    chatRecord.setSendernickname("system");
                    chatRecord.setChatType(GameConstants.CHATTYPE_SYSTEM_MSG);
                    chatRecord.setMsgContent(msg);
                    TextMessage message = new TextMessage(JSONObject.toJSONString(chatRecord));
                    MyHandler.sendMessageToUser(session,message);
                }
            }
        }
	}
}
